home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Printing / FindPrinter / CurrentPrinter.c next >
Encoding:
Text File  |  1996-01-09  |  5.7 KB  |  196 lines  |  [TEXT/MMCC]

  1. // Current Printer info sample code
  2. //
  3. // Dave Polaschek and David Hayward
  4. // Developer Technical Support
  5. // AppleLink: DEVSUPPORT
  6. //
  7. // Copyright 1995 - 1996, Apple Computer,Inc
  8. //
  9. // Check the default printer under GX or old printing architecture.
  10. // Print out the name, entityType, and zone.
  11. //
  12. // This sample is provided as-is. It's guaranteed to work on my
  13. // mac this instant, and nothing more. It shows how to determine
  14. // the currently selected printer now. Since this isn't an officially
  15. // supported endeavor, things may change in the future, and this
  16. // snippet will be for naught.
  17. //
  18. //
  19. //    You may incorporate this sample code into your applications without
  20. //    restriction, though the sample code has been provided "AS IS" and the
  21. //    responsibility for its operation is 100% yours.  However, what you are
  22. //    not permitted to do is to redistribute the source as "DSC Sample Code"
  23. //    after having made changes. If you're going to re-distribute the source,
  24. //    we require that you make it clear in the source that the code was
  25. //    descended from Apple Sample Code, but that you've made changes.
  26. //
  27.  
  28. // Revision history
  29. //
  30. // davep    8/16/95    initial cut
  31. // davep    1/9/96    cleanup and add comments
  32.  
  33.  
  34. #include <stdio.h>
  35.  
  36. #define gestaltGXVersion            'qdgx'            // in PrintingManager.h (old) or GXPrinting.h (new)
  37. #define gestaltGXPrintingMgrVersion    'pmgr'            // in PrintingManager.h (old) or GXPrinting.h (new)
  38. #define gestaltAliasMgrAttr            'alis'            // in Aliases.h
  39.  
  40. #define printingResourceID            0xE000            // 0xE000 = -8192
  41.  
  42.  
  43. OSErr        GetCurrentPrinter(Str255 printer);
  44. OSErr        test(void);
  45. Boolean        gxInstalled(void);
  46. Boolean        validPrinter(void);
  47.  
  48.  
  49. /*------------------------------------------------------------------------------*\
  50.         This function returns true if QuickDraw GX printing is available
  51. \*------------------------------------------------------------------------------*/
  52. Boolean gxInstalled(void)
  53. {
  54.     long version;
  55.  
  56.     if (Gestalt(gestaltGXVersion, &version) == noErr)
  57.         if (Gestalt(gestaltGXPrintingMgrVersion, &version) == noErr)
  58.             return(true);
  59.     return(false);
  60. }
  61.  
  62.  
  63. /*------------------------------------------------------------------------------*\
  64.         This function returns the network entity of the currently 
  65.         selected printer, and as an added bonus, you get an error code, too!
  66. \*------------------------------------------------------------------------------*/
  67. OSErr GetCurrentPrinter(Str255 printer)
  68. {
  69.     StringHandle    theDriver;
  70.     Handle            thePrinter;
  71.     OSErr            theErr;
  72.     short            resFileRefnum;
  73.  
  74.     theDriver = GetString(printingResourceID);
  75.     if (!theDriver) goto BADDriver;
  76.  
  77.     if (gxInstalled()) {
  78.         short    vRefNum;
  79.         long    dirID;
  80.         FSSpec    theFile;
  81.         long    resSize;
  82.  
  83.         theErr=FindFolder(kOnSystemDisk,kDesktopFolderType,kDontCreateFolder,&vRefNum,&dirID);
  84.         if (theErr != noErr) goto BADDriver;
  85.  
  86.         theErr=FSMakeFSSpec(vRefNum,dirID,*theDriver,&theFile);
  87.         if (theErr != noErr) goto BADDriver;
  88.  
  89.         resFileRefnum = FSpOpenResFile(&theFile,fsRdPerm);
  90.         if (resFileRefnum == -1) goto BADResFile;
  91.  
  92.         thePrinter = Get1Resource('comm', 0);
  93.         if (!thePrinter) goto BADPrinter;
  94.  
  95.         if (*((long *)*thePrinter) != 'PPTL') goto BADPrinter;
  96.  
  97.         resSize = GetHandleSize(thePrinter)-6;
  98.         BlockMoveData((*thePrinter)+6,printer,resSize);
  99.     } else {
  100.         Boolean     aliasCallsPresent;
  101.         Boolean     aliasResourcePresent;
  102.         long        resSize,response;
  103.         AliasHandle    theAlias;
  104.         
  105.         aliasCallsPresent = ((Gestalt(gestaltAliasMgrAttr,&response) == noErr) &&
  106.             (response & 1));
  107.  
  108.         theAlias = (AliasHandle) GetResource('alis',printingResourceID);
  109.         aliasResourcePresent = !!(theAlias);
  110.         
  111.         if (aliasCallsPresent && aliasResourcePresent) {
  112.             FSSpec    fileSpec;
  113.             Boolean    wasChanged;
  114.  
  115.             theErr = ResolveAlias(NULL, theAlias,&fileSpec,&wasChanged);
  116.             if (theErr != noErr) goto BADDriver;
  117.  
  118.             resFileRefnum = FSpOpenResFile(&fileSpec,fsRdPerm);
  119.             if (resFileRefnum == -1) goto BADResFile;
  120.         } else {
  121.             resFileRefnum = OpenResFile(*theDriver);
  122.             if (resFileRefnum == -1) goto BADResFile;
  123.         }
  124.         thePrinter = Get1Resource('PAPA', printingResourceID);
  125.         if (!thePrinter) goto BADPrinter;
  126.         resSize = GetHandleSize(thePrinter);
  127.         BlockMoveData(*thePrinter,printer,resSize);
  128.     }
  129.     ReleaseResource(thePrinter);
  130.     CloseResFile(resFileRefnum);
  131.  
  132.     return(noErr);
  133.  
  134. // Error returns
  135. BADPrinter:
  136.     CloseResFile(resFileRefnum);
  137. BADResFile:
  138.     theErr = ResError();
  139. BADDriver:
  140.     return(theErr);
  141. }
  142.  
  143.  
  144. /*------------------------------------------------------------------------------*\
  145.         This function is called by the test harness. Doesn't do much except
  146.         prove that things work without crashing.
  147. \*------------------------------------------------------------------------------*/
  148. OSErr test(void)
  149. {
  150.     OSErr    testValue;
  151.     Str255    printerEntity;
  152.  
  153.     testValue = GetCurrentPrinter(printerEntity);
  154.     if (testValue)
  155.         return testValue;
  156.     else {
  157.         Str31            printer,type,zone;
  158.         unsigned char    *currentString;
  159.  
  160.         currentString = printerEntity;
  161.         BlockMove(currentString,printer,Length(currentString)+1);
  162.         p2cstr(printer);
  163.         
  164.         currentString += Length(currentString) + 1;
  165.         BlockMove(currentString,type,Length(currentString) + 1);
  166.         p2cstr(type);
  167.         
  168.         currentString += Length(currentString) + 1;
  169.         BlockMove(currentString,zone,Length(currentString) + 1);
  170.         p2cstr(zone);
  171.  
  172.         printf("Current Printer name = %s\n"
  173.                 "EntityType = %s\n"
  174.                 "Zone = %s\n",printer,type,zone);
  175.         return noErr;
  176.     }
  177. }
  178.  
  179.  
  180. /*------------------------------------------------------------------------------*\
  181.         This function tells if there's a valid printer selected (i.e. not
  182.         aimed at a driver that's since been deleted or no printer selected
  183.         since last System sw install) in the chooser
  184. \*------------------------------------------------------------------------------*/
  185. Boolean validPrinter(void)
  186. {
  187.     Str255    printerEntity;
  188.     
  189.     if (GetCurrentPrinter(printerEntity) != noErr)
  190.         return false;
  191.     else if (Length(printerEntity) == 0)
  192.         return false;
  193.     else
  194.         return true;
  195. }
  196.